

//发送ajax，获取管理员信息

getAdminInfo();
function getAdminInfo(){
    $.ajax({
        url:config.url + "/getAdminInfo",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        headers:{
            "token2":localStorage.getItem("token2")
        },
        crossDomain: true,
        data: {},  //要发送的数据
        type:'GET' , // 请求的方式， 默认是get
        success: function(resp) {
            initMain(resp);
            initLeft(resp);
        }
})
}
function initMain(resp){
    console.log(resp.data.role); // 1
    var status = resp.data.role == 1 ? "管理员" : "咨询师";
    $("#top-manager")[0].innerHTML = resp.data.username + status;
    $("#top-manager1")[0].innerHTML = resp.data.username;

}
function initLeft(resp){
    var map = jsonToMap(resp.msg);
    //初始化left
    if (resp.data.role == 1){
        var admin = $('#admin');
        admin.removeClass('none');
        var str='';
        var j = 0;
        map.forEach((val,key) => {
            console.log(val.length);
            console.log(val,key);
            str+=`<div  onclick="showServiceList('user-list-item${j}')" class="first" >${key}</div>`;
            for (let i = 0; i < val.length; i++) {
                var name = val[i].split('-')[0];
                var href = val[i].split('-')[1];
                str+=`<div class="user-list-item${j} hidden" onclick="showFrame('${href}')"><span>${name}</span></div>
`;
            }
            j++;
        })
        admin[0].innerHTML = str;
    }else if (resp.data.role == 2){
        var consultant = $('#consultant');
        consultant.removeClass('none');
        var str='';
        var j = 0;
        map.forEach((val,key) => {
            console.log(val.length);
            console.log(val,key);
            if (val.length != 0){

                str+=`<div  onclick="showServiceList('manager-list-item${j}')" class="first" >${key}</div>`;
            }
            for (let i = 0; i < val.length; i++) {
                var name = val[i].split('-')[0];
                var href = val[i].split('-')[1];
                str+=`<div class="manager-list-item${j} hidden" onclick="showFrame('${href}')"><span>${name}</span></div>
`;
            }
            j++;
        })
        consultant[0].innerHTML = str;
    }
}

function showServiceList(str) {
    var display = $("." + str).css("display");
    console.log(display)
    if (display == 'flex') {

        $('.' + str).css('display', 'none');
    } else {
        $('.' + str).css('display', 'flex');

    }
}

function showFrame(src){
    $('#iframe').attr('src',src);

}

function objToStrMap(obj){
    let strMap = new Map();
    for (let k of Object.keys(obj)) {
        strMap.set(k,obj[k]);
    }
    return strMap;
}
/**
 *json转换为map
 */
function  jsonToMap(jsonStr) {
    return objToStrMap(JSON.parse(jsonStr));
}

